package com.mengfx.error;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum GlobalErrorCode {
	//
	SUCESS(200, "Success"),
	
	FAILED(300, "Failed"),
	//
	UNAUTHORIZED(401, "Unauthorized"),
	//
	NOT_FOUND(404, "Resource not found"),
	//
	EXTERNAL_ERROR(21500, "Server external error"),
	//
	INTERNAL_ERROR(500, "Server internal error"),
	//
	INVALID_ARGUMENT(11001, "Invalid argument"),
	//
	ACCOUNT_NOT_EXISTS(21000, "Account not exists"),
	PWD_NOT_MATCHED(21001, "Password not matched"),
	SMS_CODE_INVALID(21004,"SMS code is invalid"),
	ACCOUNT_EXISTS(21003, "Account is exists"),
	ALIPAY_EXISTS(21005,"Alipay is exists"),
	FILE_NOT_ALLOW_EMPTY(21008,"file not allow empty"),
	FILE_TYPE_NOT_MATCHED(21009,"file type is not matched"),
	//
	UNKNOWN(-1, "Unknown error"),
	//
	BIZ_ERROR(700, "biz error");

	private static final Map<Integer, GlobalErrorCode> values = new HashMap<Integer, GlobalErrorCode>();
	static {
		for (GlobalErrorCode ec : GlobalErrorCode.values()) {
			values.put(ec.errorCode, ec);
		}
	}

	private int errorCode;
	private String error;

	private GlobalErrorCode(int errorCode, String error) {
		this.errorCode = errorCode;
		this.error = error;
	}

	public static GlobalErrorCode valueOf(int code) {
		GlobalErrorCode ec = values.get(code);
		if (ec != null)
			return ec;
		return UNKNOWN;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public String getError() {
		return error;
	}
}
